<?php
class UsersController extends AppController {

	var $name = 'Users';
	//var $uses = array('Caja','User');
	var $components = array('Acl', 'Auth', 'Session');
	var $helpers = array('Form', 'Html', 'Javascript', 'Time');
    var $paginate = array('limit' => 20, 'order' => array( 'User.id' => 'asc'));
function thaidy() {	

}


function login() {
	
	if ($this->Session->read('Auth.User')) {
		$this->Session->setFlash('You are logged in!');
		$this->User->id = $this->Auth->user('id');
		$this->User->saveField('last_login', date('Y-m-d H:i:s') );
		}

	}
	function perfil($id = null) {
		$id = $this->Auth->user('id');
        $this->set('user',$this->User->read(null,$id));
        $this->set('caja',$this->User->Caja->find('all', array('conditions' => array( 'abierta ' => '1','user_id' => $id))));
        
	}
	function pass() {
		
		
	}
	function change_password() {

		$this->Session->setFlash('Clave actualizada');
		$this->redirect(array('action' => 'perfil'));
	}
	function logout() {
    $this->Session->destroy();
    $this->Session->setFlash('Usted ha salido del sistema');
    $this->redirect(array('action' => 'login'));

	}
	function index() {
		$this->User->recursive = 0;
		$this->set('grupo',$this->Auth->user('group_id'));
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
    //$this->Acl->allow($aroAlias, $acoAlias);

		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
            $this->data['User']['activo'] = '1';
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('Este usuario ha sido guardado', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Este usuario no ha sido guardado. Por favor, intente de nuevo.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Usuario Invalido', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('Usuario actualizado', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Este usuario no ha sido actualizado. Por favor intente de nuevo.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

    function initDB() {
    $group =& $this->User->Group;
    //Allow admins to everything
    $group->id = 1;
    $this->Acl->allow($group, 'controllers');

    //allow managers to posts and widgets
    $group->id = 2;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Cajas');
    $this->Acl->allow($group, 'controllers/Users/perfil');
	$this->Acl->allow($group, 'controllers/Users/pass');
    //allow users to only add and edit on posts and widgets
    $group->id = 3;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Posts/add');
    $this->Acl->allow($group, 'controllers/Posts/edit');
    $this->Acl->allow($group, 'controllers/Widgets/add');
    $this->Acl->allow($group, 'controllers/Widgets/edit');
    //we add an exit to avoid an ugly "missing views" error message
    echo "all done";
    exit;
    }
	    
		function beforeFilter() {
		//$this->Acl->allow($aroAlias, $acoAlias);
   		 parent::beforeFilter();
    		    	
			$this->Auth->allow(array('*'));

    	}



}
?>
